<template>
  <div class="xiaomi-store">
    <div class="content-wrapper">
      <!-- 竖直标签栏 -->
      <nav class="category-tabs">
        <button v-for="(item, index) in classification" :key="index" @click="setActiveCategory(index, item.category_id)"
          :class="['category-tabs__item', { 'category-tabs__item--active': activeCategory === index }]">
          {{ item.category_name }}
        </button>
      </nav>

      <!-- 产品列表 -->
      <div class="product-list">
        <div v-for="product in categories" :key="product.id" class="product-card">
          <img :src="product.puzzle_url" :alt="product.name" class="product-card__image" />
          <div class="product-card__content">
            <h3 class="product-card__name">{{ product.name }}</h3>
            <p class="product-card__description">{{ product.product_desc_origin | setContent }}</p>
            <p class="product-card__price">¥{{ product.price }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { classify, comments } from '../api/index'

export default {
  name: 'XiaomiMobileStoreCategories',
  data() {
    return {
      id: 1242,
      activeCategory: 1, // 如果分类以0为基准，这里设为0；如果需要从1开始，请设为1并调整相关逻辑
      classification: [],
      categories: []
    }
  },
  
  async created() {
    const content = await classify();
    let { code, data } = content;
    if (code === 0) {
      this.classification = data;
      // 初始化时加载第一类（索引为0的类别）
      await this.setActiveCategory(1, this.classification[1].category_id);
    }
  },
  
  filters: {
    setContent(value) {
      return value.slice(0, 37) + '...';
    },
  },

  methods: {
    async setActiveCategory(index, category_id) {
      this.activeCategory = index;
      const commentsData = await comments({ category_id });
      let { code, data } = commentsData;
      if (code === 0) {
        this.categories = data.product;
      }
    },
  }
}
</script>


<style scoped>
.xiaomi-store {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  color: #333;
  background-color: #f5f5f5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.search-bar {
  display: flex;
  padding: 10px;
  background-color: #fff;
  position: sticky;
  top: 0;
  z-index: 10;
}

.search-bar__input {
  flex: 1;
  padding: 8px 12px;
  font-size: 14px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
  outline: none;
}

.search-bar__button {
  background-color: #ff6900;
  color: white;
  border: none;
  padding: 0 15px;
  cursor: pointer;
  border-radius: 0 4px 4px 0;
}

.content-wrapper {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.category-tabs {
  width: 80px;
  background-color: #f5f5f5;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.category-tabs__item {
  width: 100%;
  padding: 15px 5px;
  font-size: 12px;
  border: none;
  background: none;
  color: #333;
  cursor: pointer;
  transition: all 0.3s;
  text-align: center;
  white-space: nowrap;
}

.category-tabs__item--active {
  color: #ff6900;
  font-weight: bold;
  background-color: #fff;
  border-left: 2px solid #ff6900;
}

.product-list {
  flex: 1;
  padding: 10px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.product-card {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 15px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.product-card__image {
  width: 100%;
  height: 150px;
  /* 填充图片 */
  object-fit: cover;
  padding: 5px;
}

.product-card__content {
  padding: 10px;
}

.product-card__name {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 4px;
}

.product-card__description {
  font-size: 12px;
  color: #666;
  margin-bottom: 6px;
}

.product-card__price {
  font-size: 16px;
  font-weight: bold;
  color: #ff6900;
}

@media (min-width: 768px) {
  .category-tabs {
    width: 100px;
  }

  .category-tabs__item {
    font-size: 14px;
  }

  .product-list {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }

  .product-card {
    margin-bottom: 0;
  }
}
</style>